package youleche.com.sql.servlet.common;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 方法生成工厂
 * @author Administrator
 *
 */
public class MethodGenerateFactory {

	/**
	 * 得到方法字符串
	 * @param type 方法类型（0-添加）
	 * @param sql sql语句
	 * @param comment 注释
	 * @return 返回方法字符串 
	 */
	public static String getMethod(int type, String sql, Map<String, String> comment){
		switch(type){
		case 0: 
			//添加
			return add(sql);
		}
		return "";
	}
	
	/**
	 * 添加方法
	 * @param sql insert-sql语句
	 * @return 返回方法字符串
	 */
	private static String add(String sql){
		
		String setObjectStr = getAddPreparedStatementSetObject(matchField(sql), matchTableName(sql));
		String commentStr = "/**\n * 添加车辆信息\n * @param con 数据库连接\n * @param vehicleInfo 车辆信息\n * @return 返回影响的行数\n * @throws Exception 异常\n */\n@Override\n";
		String functionStr = "public int add"+upcaseTableName(sql)+"(Connection con, MVehicleInfo vehicleInfo) throws Exception {\n";
		String end = "\n}\n";
		String sqlStr= "\tString sql = \"" + sql + "\";\n";
		return commentStr + functionStr + sqlStr + setObjectStr + end;
	}
	
	/**
	 * 匹配出字段
	 * @param sql sql语句
	 * @return 字段
	 */
	private static String matchField(String sql){
		Pattern pattern = Pattern.compile("\\((.*?)\\)");
		Matcher m = pattern.matcher(sql);
		String fields = "";
		while(m.find()){
			fields = m.group(1);
			break;
		}
		
		return fields;
	}
	
	/**
	 * 表名的第一个字母转换为大写
	 * @param sql sql语句
	 * @return 返回表名
	 */
	private static String upcaseTableName(String sql){
		String tableName = matchTableName(sql);
		return tableName.substring(0, 1) + tableName.substring(1, tableName.length());
	}
	/**
	 * 匹配出表名
	 * @param sql sql语句
	 * @return 表名
	 */
	private static String matchTableName(String sql){
		Pattern pattern = Pattern.compile(" (.*?)\\(");
		Matcher m = pattern.matcher(sql);
		String tableName = "";
		while(m.find()){
			tableName = m.group(1);
			break;
		}
		
		return tableName;
	}
	
	/**
	 * 得到PreparedStatement赋值内容
	 * @param fields 字段
	 * @param tableName 表名
	 * @return 返回内容
	 */
	private static String getAddPreparedStatementSetObject(String fields, String tableName){
		String[] fieldArr = fields.split(",");
		StringBuilder sb = new StringBuilder();
		sb.append("\tPreparedStatement pre = con.prepareStatement(sql);\n");
		int index = 1;
		String upcaseField = "";
		for (String field : fieldArr) {
			upcaseField = field.substring(0, 1).toUpperCase() + field.substring(1, field.length());
			sb.append("\tpre.setObject(" + index + ", " + tableName + ".get" + upcaseField + "());\n");
			index ++;
		}
		sb.append("\n");
		sb.append("\treturn pre.executeUpdate();");
		
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String sql = "INSERT rentVehicleInfo(rentId,releaseId,rentTitle,rentDesc,vehicleType,rentType,linkMan,linkPhone,imageId,isDelete,rentPrice,rentAddress,shopName,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15,A16,A17) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
		
		String r = add(sql);
		System.out.println(r);
	}
}
